package com.wman.drawing.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.wman.sheep.common.utils.DensityUtil;

import androidx.annotation.Nullable;

/**
 * @ProjectName: wman
 * @Package: com.wman.drawing.view
 * @ClassName: PieChart
 * @Description: java类作用描述
 * @Author: wman
 * @CreateDate: 2019/11/5 16:37
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/11/5 16:37
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class PieChart extends View {
    private static final int RADIUS = DensityUtil.dip2px(150);
    private static final int LENGTH = DensityUtil.dip2px(10);
    private static final int PULLED_OUT_INDEX = 2;

    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private RectF mBounds = new RectF();

    private int[] mAngles = {60, 100, 120, 80};
    private int[] mColors = {Color.parseColor("#2979FF"),
            Color.parseColor("#C2185B"),
            Color.parseColor("#009688"),
            Color.parseColor("#F80880")};

    public PieChart(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mBounds.set(getWidth() / 2 - RADIUS, getHeight() / 2 - RADIUS,
                getWidth() / 2 + RADIUS, getHeight() / 2 + RADIUS);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int currentAngle = 0;
        for (int i = 0; i < mAngles.length; i++) {
            mPaint.setColor(mColors[i]);
            canvas.save();
            if (i == PULLED_OUT_INDEX) {
                canvas.translate((float) Math.cos(Math.toRadians(currentAngle + mAngles[i] / 2)) * LENGTH,
                        (float) Math.sin(Math.toRadians(currentAngle + mAngles[i] / 2)) * LENGTH);
            }
            canvas.drawArc(mBounds, currentAngle, mAngles[i], true, mPaint);
            canvas.restore();
            currentAngle += mAngles[i];
        }
    }
}
